﻿using GalaSoft.MvvmLight;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TorrentViewModel : ViewModelBase
    {

        public RelayCommand StartTorrent { get; private set; }

        public RelayCommand ForceStartTorrent { get; private set; }

        public RelayCommand StopTorrent { get; private set; }

        public RelayCommand RemoveTorrent { get; private set; }

        public RelayCommand RemoveDataTorrent { get; private set; }

        public RelayCommand SaveTorrent { get; private set; }

        /// <summary>
        /// The <see cref="SelectedTorrent" /> property's name.
        /// </summary>
        public const string SelectedTorrentPropertyName = "SelectedTorrent";

        private Torrent _selectedTorrent = null;

        /// <summary>
        /// Gets the SelectedTorrent property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Torrent SelectedTorrent
        {
            get
            {
                return _selectedTorrent;
            }

            set
            {
                if (_selectedTorrent == value)
                {
                    return;
                }

                var oldValue = _selectedTorrent;
                _selectedTorrent = value;

                // Remove one of the two calls below
                //throw new NotImplementedException();

                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedTorrentPropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(SelectedTorrentPropertyName, oldValue, value, true);
            }
        }

        /// <summary>
        /// Initializes a new instance of the TorrentViewModel class.
        /// </summary>
        public TorrentViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                SelectedTorrent = new Torrent();
               
            }
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}
            Messenger.Default.Register<Torrent>(this, "SetTorrent", SetSelectedTorrent);


            StartTorrent = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.TorrentStart(SelectedTorrent);
                GoBack();
            }, () =>
            {
                if ((SelectedTorrent.Status & TorrentStatus.Started) != TorrentStatus.Started
                    || SelectedTorrent.StatusMessage.StartsWith("[F]"))
                    return true;
                else
                    return false;
            });

            ForceStartTorrent = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.TorrentForceStart(SelectedTorrent);
                GoBack();
            }, () =>
            {
                if ((SelectedTorrent.Status & TorrentStatus.Started) != TorrentStatus.Started
                    || !SelectedTorrent.StatusMessage.StartsWith("[F]"))
                    return true;
                else
                    return false;
            });

            StopTorrent = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.TorrentStop(SelectedTorrent);
                GoBack();
            }, () =>
            {
                if ((SelectedTorrent.Status & TorrentStatus.Started) == TorrentStatus.Started)
                    return true;
                else
                    return false;
            });

            RemoveTorrent = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.TorrentRemove(SelectedTorrent);
                GoBack();
            }, () =>
            {
                return true;
            });

            RemoveDataTorrent = new RelayCommand(() =>
            {
                ViewModelLocator.DataService.TorrentRemove(SelectedTorrent, true);
                GoBack();
            }, () =>
            {
                return true;
            });

            SaveTorrent = new RelayCommand(() =>
            {
                SelectedTorrent.Label = SelectedTorrent.Label;
                GoBack();
                
            }, () =>
            {
                return true;
            });
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        public void SetSelectedTorrent(Torrent st)
        {
            this.SelectedTorrent = st;
        }

        private void GoBack()
        {
            ViewModelLocator.GoBack();
        }
    }
}